/*
 * rtc.c
 *
 *  Created on: Sep 11, 2013
 *      Author: Ken Arok
 *
 * \brief The driver of RTC Device.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "rtc_hal.h"
#include "rtc.h"



void vWriteRTC(rtc_string_t *_rtc_string)
{
	rtc_raw_t _raw_rtc;
	uint8_t _tmp;

	/* Seconds. */
	_tmp = (_rtc_string->time[6] - '0') << 4;
	_raw_rtc.seconds  = (_rtc_string->time[7] - '0') | _tmp;
	/* Minutes. */
	_tmp = (_rtc_string->time[3] - '0') << 4;
	_raw_rtc.minutes  = (_rtc_string->time[4] - '0') | _tmp;
	/* Hours. */
	_tmp = (_rtc_string->time[0] - '0') << 4;
	_raw_rtc.hours  = (_rtc_string->time[1] - '0') | _tmp;
	/* Days. */
	_tmp = (_rtc_string->date2[0] - '0') << 4;
	_raw_rtc.days  = (_rtc_string->date2[1] - '0') | _tmp;
	/* Months. */
	_tmp = (_rtc_string->date2[3] - '0') << 4;
	_raw_rtc.months  = (_rtc_string->date2[4] - '0') | _tmp;
	/* Years. */
	_tmp = (_rtc_string->date2[8] - '0') << 4;
	_raw_rtc.years  = (_rtc_string->date2[9] - '0') | _tmp;

	/* Write RTC value to chip. */
	rtc_write_calendar(&_raw_rtc);
}

void vReadRTC(rtc_string_t *_rtc_string)
{
	rtc_raw_t _raw_rtc;

	/* Read RTC value from chip. */
	rtc_read_calendar(&_raw_rtc);

	/* Format date type 1, i.e Aug 21, 2013. */
	switch(_raw_rtc.months) {
		case 0x01:
			_rtc_string->date1[0] = 'J';
			_rtc_string->date1[1] = 'a';
			_rtc_string->date1[2] = 'n';
		break;

		case 0x02:
			_rtc_string->date1[0] = 'F';
			_rtc_string->date1[1] = 'e';
			_rtc_string->date1[2] = 'b';
		break;

		case 0x03:
			_rtc_string->date1[0] = 'M';
			_rtc_string->date1[1] = 'a';
			_rtc_string->date1[2] = 'r';
		break;

		case 0x04:
			_rtc_string->date1[0] = 'A';
			_rtc_string->date1[1] = 'p';
			_rtc_string->date1[2] = 'r';
		break;

		case 0x05:
			_rtc_string->date1[0] = 'M';
			_rtc_string->date1[1] = 'a';
			_rtc_string->date1[2] = 'y';
		break;

		case 0x06:
			_rtc_string->date1[0] = 'J';
			_rtc_string->date1[1] = 'u';
			_rtc_string->date1[2] = 'n';
		break;

		case 0x07:
			_rtc_string->date1[0] = 'J';
			_rtc_string->date1[1] = 'u';
			_rtc_string->date1[2] = 'l';
		break;

		case 0x08:
			_rtc_string->date1[0] = 'A';
			_rtc_string->date1[1] = 'u';
			_rtc_string->date1[2] = 'g';
		break;

		case 0x09:
			_rtc_string->date1[0] = 'S';
			_rtc_string->date1[1] = 'e';
			_rtc_string->date1[2] = 'p';
		break;

		case 0x10:
			_rtc_string->date1[0] = 'O';
			_rtc_string->date1[1] = 'c';
			_rtc_string->date1[2] = 't';
		break;

		case 0x11:
			_rtc_string->date1[0] = 'N';
			_rtc_string->date1[1] = 'o';
			_rtc_string->date1[2] = 'v';
		break;

		case 0x12:
			_rtc_string->date1[0] = 'D';
			_rtc_string->date1[1] = 'e';
			_rtc_string->date1[2] = 'c';
		break;

		default: break;
	}
	_rtc_string->date1[3] = ' ';
	_rtc_string->date1[4] = (_raw_rtc.days >> 4) + '0';
	_rtc_string->date1[5] = (_raw_rtc.days & 0x0F) + '0';
	_rtc_string->date1[6] = ',';
	_rtc_string->date1[7] = ' ';
	_rtc_string->date1[8] = '2';
	_rtc_string->date1[9] = '0';
	_rtc_string->date1[10] = (_raw_rtc.years >> 4) + '0';
	_rtc_string->date1[11] = (_raw_rtc.years & 0x0F) + '0';
	_rtc_string->date1[12] = '\0';

	/* Format date type 1, i.e 21/08/2013. */
	_rtc_string->date2[0] = (_raw_rtc.days >> 4) + '0';
	_rtc_string->date2[1] = (_raw_rtc.days & 0x0F) + '0';
	_rtc_string->date2[2] = '/';
	_rtc_string->date2[3] = (_raw_rtc.months >> 4) + '0';
	_rtc_string->date2[4] = (_raw_rtc.months & 0x0F) + '0';
	_rtc_string->date2[5] = '/';
	_rtc_string->date2[6] = '2';
	_rtc_string->date2[7] = '0';
	_rtc_string->date2[8] = (_raw_rtc.years >> 4) + '0';
	_rtc_string->date2[9] = (_raw_rtc.years & 0x0F) + '0';
	_rtc_string->date2[10] = '\0';

	/* Format time, i.e 12:24:45 */
	_rtc_string->time[0] = (_raw_rtc.hours >> 4) + '0';
	_rtc_string->time[1] = (_raw_rtc.hours & 0x0F) + '0';
	_rtc_string->time[2] = ':';
	_rtc_string->time[3] = (_raw_rtc.minutes >> 4) + '0';
	_rtc_string->time[4] = (_raw_rtc.minutes & 0x0F) + '0';
	_rtc_string->time[5] = ':';
	_rtc_string->time[6] = (_raw_rtc.seconds >> 4) + '0';
	_rtc_string->time[7] = (_raw_rtc.seconds & 0x0F) + '0';
	_rtc_string->time[8] = '\0';
}

